<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use Ramsey\Uuid\Uuid;
use think\Cookie;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        // 检查是否存在名为"uuid"
        $uuid = $this->request->cookie('uuid');
        if (empty($uuid)) {
            // 生成新的 UUID
            $uuid = Uuid::uuid4()->toString();
            // 将 UUID 存储在 Cookie 中，有效期为 30 天
            Cookie::set('uuid', $uuid, 30*24*60*60); // 30 天有效期
        }
        $this->assign('uuid',$uuid);
        
        //基础信息展示
        //剩余量
        $count = Db::name('kami')->where('status',0)->count();
        $this->assign('count',$count);
        return $this->view->fetch();
    }

    public function pick()
    {
        //验证码验证
        $params = $this->request->param();
        if(!captcha_check($params['captcha'])){
            $data = [
                'code' => 201,
                'msg' =>'验证码错误'
            ];
            return json($data);
        }

        //获取ip
        $ip = $this->request->ip();

        $startDate = date('Y-m-d H:i:s', strtotime('-10 days'));
        // 构建查询对象
        $query = Db::name('fenfa')->where('time', '>=', $startDate);

        //邮箱查询
        $query->where('email',$params['email']);
        $result = $query->select();
        if($result){
            $data = [
                'code' => 201,
                'msg' =>'该邮箱已经领取过啦~'
            ];
            return json($data);
        }
        //浏览器查询
        $query->where('browser',$params['csrf_token']);
        $result = $query->select();
        if($result){
            $data = [
                'code' => 202,
                'msg' =>'当前设备领取上限~'
            ];
            return json($data);
        }
        //ip查询
        $query->where('ip',$ip);
        $result = $query->select();
        if($result){
            $data = [
                'code' => 203,
                'msg' =>'当前ip领取上限~'
            ];
            return json($data);
        }

        //开始发送卡密啦
        // 查询一条记录并更新状态为1
        $record = Db::name('kami')->where('status', 0)->find();
        if ($record) {
            $kami = $record['kami'];
            Db::name('kami')->where('id', $record['id'])->update(['status' => 1]);
        }


        //发送邮箱
         // 渲染模板并发送邮件
        $template_data = [
            'kami' => $kami
        ];
        $html_content = view('email', $template_data)->getContent();

        $obj = \app\common\library\Email::instance();
        $result = $obj
                ->to($params['email'])
                ->subject('乞丐卡密分发系统提醒您：系统收来自的反馈信息' ) //右键主题
                // ->attachment($path, $name) //这里是多张图片，数量不确定，这里是配置附件，可以不用
                ->message($html_content)
                ->send();

        //记录到分发表中
        $fenfa = [
            'email' => $params['email'],
            'kami_id' => $record['id'], //取出来记录的id
            'time' => time(),
            'ip' => $ip,
            'browser' => $params['csrf_token']
        ];
        Db::name('fenfa')->insert($fenfa);


        $data = [
            'code' => 200,
            'msg' =>'领取成功,请到邮箱查看~'
        ];
        return json($data);
    }

}
